package com.eptok.gw.szcups.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TripleDES {

	private static final String	ALGORITHM	= "DESede";					// 3DES算法
	private static final String	PADDING		= "DESede/ECB/Nopadding";	// 补位方式

	/**
	 * 3DES加密
	 * @param src 明文
	 * @param key 24字节的密钥
	 * @return 密文
	 * @throws Exception
	 */
	public static byte[] encrypt(final byte[] src, final byte[] key) throws Exception {
		final SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
		final Cipher cipher = Cipher.getInstance(PADDING);
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		return cipher.doFinal(src);
	}

	/**
	 * 3DES解密
	 * @param src 密文
	 * @param key 24字节的密钥
	 * @return 明文
	 * @throws Exception
	 */
	public static byte[] decrypt(final byte[] src, final byte[] key) throws Exception {
		final SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
		final Cipher cipher = Cipher.getInstance(PADDING);
		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		return cipher.doFinal(src);
	}
}
